package com.kiwi.cases.match.usermatch;

import com.kiwi.util.APIEncryptSet;
import com.kiwi.util.EncryptSet.Impl.SignatureServiceImpl;
import com.kiwi.util.ConcatUtil;
import jxl.read.biff.BiffException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import static com.kiwi.constant.Constant.COMMON_GSON;
import static com.kiwi.constant.ConstantURL.GAEA_USER_MATCH_DIR;
import static com.kiwi.util.excel.JxlReader.jxlReader;

/**
 * Created by Administrator on 2018/4/17.
 */
public class GenerateParams {
    //accountId;SessionId;userMatchOnConditionVo
    //返回参数:未加密的字符串
    //accountId=10006&sessionId=db02e51a20c014a929d41241e08398aa&type=0&userMatchOnConditionVo={"age":75,"sex":1,"accountId":"10006","latitude":0,"longitude":0}

    /**TODO:改写此类match数据
     * 功能：/user/match接口参数
     * @param param excel一行数据
     * @return 未加密httpParam
     * @author hancongcong
     */
    public static String getMatchParam(HashMap<String,String> param ){
        HashMap<String,String> apiParam = new HashMap<String,String>();
        apiParam.put("accountId",param.get("accountId"));
        String sessionId = "";
        if(param.get("sessionId")==null ||param.get("sessionId")==""){
            sessionId = "helloworld";
        }else{
            sessionId = param.get("sessionId");
        }
        apiParam.put("sessionId",sessionId);
        apiParam.put("type",param.get("type"));
        UserMatchOnConditionVo userMatchOnConditionVo = new UserMatchOnConditionVo();
        userMatchOnConditionVo.setAge(Integer.parseInt(param.get("age")));
        userMatchOnConditionVo.setSex(Integer.parseInt(param.get("sex")));
        userMatchOnConditionVo.setAccountId(param.get("accountId"));
        userMatchOnConditionVo.setLatitude(Double.parseDouble(param.get("latitude")));
        userMatchOnConditionVo.setLongitude(Double.parseDouble(param.get("longitude")));
        String userMatchOnConditionStr = COMMON_GSON.toJson(userMatchOnConditionVo);
        apiParam.put("userMatchOnConditionVo",userMatchOnConditionStr);
        String apiStr = ConcatUtil.concatParams(apiParam);
        System.out.println(apiStr);
        return apiStr;
    }

    /**TODO：改写此类result结果
     *功能：/user/match/result接口参数
     * @param param excel一行数据
     * @return 返回accountId和SessionId
     */
    public static String getResultParam(HashMap<String,String> param){
        HashMap<String,String> apiParam = new HashMap<String,String>();
        apiParam.put("accountId",param.get("accountId"));
        String sessionId = "";
        if(param.get("sessionId")==null ||param.get("sessionId")==""){
            sessionId = "helloworld";
        }else{
            sessionId = param.get("sessionId");
        }
        apiParam.put("sessionId",param.get("sessionId"));
        String apiStr = ConcatUtil.concatParams(apiParam);
        System.out.println(apiStr);
        return apiStr;
    }
    /**
     *功能：返回期望值
     * @param param excel一行数据
     * @return 返回EXPECT
     */
    public static String getExpect(HashMap<String,String> param){
        return param.get("EXPECT")+","+param.get("accountId");
    }

    //TODO:返回match接口参数
     //返回api-match参数httpParams---加密字符串数组Iterator<Object[]>
    public static Iterator<Object[]> getMatchParams(ArrayList<HashMap<String,String>> params) throws IOException {
        HashMap<String,String> apiParam = new HashMap<String,String>();
        List<Object> item = new ArrayList<Object>();
        for(int i = 0;i<params.size();i++){
            apiParam = params.get(i);
            item.add(getMatchParam(apiParam));
        }
        System.out.println(item);

        List<Object[]> strs = new ArrayList<Object[]>();

        for (Object u : item) {
            //做一个形式转换
            strs.add(new Object[] { u });
        }
        return strs.iterator();
    }
    //TODO:返回match-result接口参数
    //返回api-result接口参数httpParams---未加密参数和期望值
    public static Iterator<Object[]> getMatchResultParams(ArrayList<HashMap<String,String>> params) throws IOException {
        HashMap<String,String> apiParam = new HashMap<String,String>();

        List<Object> item = new ArrayList<Object>();
        for(int i = 0;i<params.size();i++){
            HashMap<String,String> paramAndExpect = new HashMap<String,String>();
            apiParam = params.get(i);
            paramAndExpect.put("param",getResultParam(apiParam));
            paramAndExpect.put("expect",apiParam.get("EXPECT")+","+apiParam.get("accountId"));
            item.add(paramAndExpect);
        }
        System.out.println(item);
        List<Object[]> strs = new ArrayList<Object[]>();

        for (Object u : item) {
            //做一个形式转换
            strs.add(new Object[] { u });
        }
        return strs.iterator();
    }

   /* public static ArrayList<HashMap<String,String>> generateParams(ArrayList<HashMap<String,String>> params) throws IOException {
        //FileWriter fw = new FileWriter(fileName);
        HashMap<String,String> apiParam = new HashMap<String,String>();
        ArrayList<HashMap<String,String>> apiParams = new ArrayList<HashMap<String,String>>();
        for(int i = 0;i<params.size();i++){
            apiParam = params.get(i);
            apiParams.add(getEncodePamram(getApiParam(apiParam)));
        }
        //fw.close();
        System.out.println("httpParams:"+apiParams);
        return apiParams;
    }*/


    //返回data,stamp,uid,tk
    public static HashMap<String,String> getEncodePamram(String apiParam) throws UnsupportedEncodingException {
        HashMap<String,String> paramMap = new HashMap<String,String>();
        String apiParamEncode = APIEncryptSet.enCode(apiParam);
        String[] paramsEncode = apiParamEncode.split("&");
        String data = paramsEncode[0].substring(5,paramsEncode[0].length());
        String stamp = paramsEncode[1].substring(6,paramsEncode[1].length());
        String[] params = apiParam.split("&");
        paramMap.put("data",data);
        paramMap.put("stamp",stamp);
        String uid = "";
        String tk = "";
        for(int i = 0;i<params.length;i++){
            if(params[i].contains("accountId=")){
                String[] accountId =params[i].split("=");
                uid = accountId[1];
                SignatureServiceImpl ss = new SignatureServiceImpl();
                tk  = ss.encryptWapSession(uid,"helloworld");
                paramMap.put("uid",uid);
                paramMap.put("tk",tk);
            }
        }
        return paramMap;
    }
    //返回值：参数字符串、accountId、tk(cookie)
    public static void main(String[] args) throws IOException, BiffException{
        String xlsPath = GAEA_USER_MATCH_DIR +"/test.xls";
        String txtPath = GAEA_USER_MATCH_DIR +"/test.txt";
        //getEncodeResultParams(jxlReader(xlsPath,"Test0"));
       // getEncodeResultParams(jxlReader(xlsPath,"Test1"));
        String x ="abcde";
        ArrayList<Object[]> test = new ArrayList<Object[]>();
        test.add(new Object[]{"abc"});
        test.add(new Object[]{"bcd"});
        test.add(new Object[]{"dfafdasf"});
        Object[] save = test.toArray();

}
}
